package cat20032010;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.Timer;

/**
 *
 * @author aDeeY
 */
public class AddOrder extends javax.swing.JFrame {
    public static Vector menuCodeList = new Vector();
    Timer timer = new Timer(1000, new TimerListener());
    Statement stmt;
    ResultSet rset;
    double ttlprice;

    public AddOrder() throws SQLException {
        initComponents();
        setLocationRelativeTo(null);
        getMenuCode();
    }

    AddOrder(String str) {
        initComponents();
        setLocationRelativeTo(null);
        update();
        timer.start();
        insertTableNum(str);
        insertMenuCode();
    }
    
    public void insertMenuCode(){
        try {
            menuCodeList = new Vector();
            menuCodeList.add("Select Menu Code");

            Main.getConnection();
             
            Statement stmt = Main.con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
            rset = stmt.executeQuery("SELECT MENU_CODE FROM MENU");
           
        } catch (SQLException ex) {
        }
        try {
            while (rset.next()) {
                menuCodeList.add(rset.getString(1));
                
            }
        } catch (SQLException ex) {
            Logger.getLogger(Waiter.class.getName()).log(Level.SEVERE, null, ex);
        }
        jcbMenu.setModel(new javax.swing.DefaultComboBoxModel(menuCodeList));
        
        jcbMenu.addActionListener(new java.awt.event.ActionListener(){
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jcbMenuActionPerformed(evt);
            }
        });
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jpPanel = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        jbtnDone = new javax.swing.JButton();
        jLabel7 = new javax.swing.JLabel();
        jlTable = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jtfTotalPrice = new javax.swing.JTextField();
        jcbMenu = new javax.swing.JComboBox();
        jtfMName = new javax.swing.JTextField();
        jtfQty = new javax.swing.JTextField();
        jtfTime = new javax.swing.JTextField();
        jtfDate = new javax.swing.JTextField();
        jtfTable = new javax.swing.JTextField();
        jbtnCancel = new javax.swing.JButton();
        background = new javax.swing.JLabel();

        setTitle("ADD ORDER");
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setResizable(false);

        jpPanel.setLayout(null);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Add Order", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 24))); // NOI18N
        jPanel1.setLayout(null);

        jbtnDone.setIcon(new javax.swing.ImageIcon(getClass().getResource("/cat20032010/DONE.png"))); // NOI18N
        jbtnDone.setBorderPainted(false);
        jbtnDone.setContentAreaFilled(false);
        jbtnDone.setDefaultCapable(false);
        jbtnDone.setFocusPainted(false);
        jbtnDone.setRolloverIcon(new javax.swing.ImageIcon(getClass().getResource("/cat20032010/DONErollover.png"))); // NOI18N
        jbtnDone.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnDoneActionPerformed(evt);
            }
        });
        jPanel1.add(jbtnDone);
        jbtnDone.setBounds(450, 350, 100, 60);

        jLabel7.setFont(new java.awt.Font("Verdana", 0, 16)); // NOI18N
        jLabel7.setText("Total Price   :");
        jPanel1.add(jLabel7);
        jLabel7.setBounds(70, 290, 120, 30);

        jlTable.setFont(new java.awt.Font("Verdana", 0, 16)); // NOI18N
        jlTable.setText("Table No.     :");
        jPanel1.add(jlTable);
        jlTable.setBounds(70, 50, 120, 30);

        jLabel2.setFont(new java.awt.Font("Verdana", 0, 16)); // NOI18N
        jLabel2.setText("Menu Code   :");
        jPanel1.add(jLabel2);
        jLabel2.setBounds(70, 90, 120, 30);

        jLabel3.setFont(new java.awt.Font("Verdana", 0, 16)); // NOI18N
        jLabel3.setText("Menu Name  :");
        jPanel1.add(jLabel3);
        jLabel3.setBounds(70, 130, 120, 30);

        jLabel4.setFont(new java.awt.Font("Verdana", 0, 16)); // NOI18N
        jLabel4.setText("Quantity      :");
        jPanel1.add(jLabel4);
        jLabel4.setBounds(70, 170, 120, 30);

        jLabel5.setFont(new java.awt.Font("Verdana", 0, 16)); // NOI18N
        jLabel5.setText("Time           :");
        jPanel1.add(jLabel5);
        jLabel5.setBounds(70, 210, 120, 30);

        jLabel6.setFont(new java.awt.Font("Verdana", 0, 16)); // NOI18N
        jLabel6.setText("Date           :");
        jPanel1.add(jLabel6);
        jLabel6.setBounds(70, 250, 120, 30);

        jtfTotalPrice.setEditable(false);
        jtfTotalPrice.setFont(new java.awt.Font("Verdana", 0, 16)); // NOI18N
        jPanel1.add(jtfTotalPrice);
        jtfTotalPrice.setBounds(230, 290, 260, 30);

        jcbMenu.setFont(new java.awt.Font("Verdana", 0, 16)); // NOI18N
        jcbMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jcbMenuActionPerformed(evt);
            }
        });
        jPanel1.add(jcbMenu);
        jcbMenu.setBounds(230, 90, 260, 30);

        jtfMName.setFont(new java.awt.Font("Verdana", 0, 16)); // NOI18N
        jPanel1.add(jtfMName);
        jtfMName.setBounds(230, 130, 260, 30);

        jtfQty.setFont(new java.awt.Font("Verdana", 0, 16)); // NOI18N
        jtfQty.addInputMethodListener(new java.awt.event.InputMethodListener() {
            public void caretPositionChanged(java.awt.event.InputMethodEvent evt) {
            }
            public void inputMethodTextChanged(java.awt.event.InputMethodEvent evt) {
                jtfQtyInputMethodTextChanged(evt);
            }
        });
        jtfQty.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jtfQtyKeyPressed(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jtfQtyKeyReleased(evt);
            }
        });
        jPanel1.add(jtfQty);
        jtfQty.setBounds(230, 170, 260, 30);

        jtfTime.setEditable(false);
        jtfTime.setFont(new java.awt.Font("Verdana", 0, 16)); // NOI18N
        jPanel1.add(jtfTime);
        jtfTime.setBounds(230, 210, 260, 30);

        jtfDate.setEditable(false);
        jtfDate.setFont(new java.awt.Font("Verdana", 0, 16)); // NOI18N
        jPanel1.add(jtfDate);
        jtfDate.setBounds(230, 250, 260, 30);

        jtfTable.setEditable(false);
        jtfTable.setFont(new java.awt.Font("Verdana", 0, 16)); // NOI18N
        jPanel1.add(jtfTable);
        jtfTable.setBounds(230, 50, 260, 30);

        jbtnCancel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/button/CANCEL.png"))); // NOI18N
        jbtnCancel.setBorderPainted(false);
        jbtnCancel.setContentAreaFilled(false);
        jbtnCancel.setDefaultCapable(false);
        jbtnCancel.setFocusPainted(false);
        jbtnCancel.setRolloverIcon(new javax.swing.ImageIcon(getClass().getResource("/button/CANCELMouseOver.png"))); // NOI18N
        jbtnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnCancelActionPerformed(evt);
            }
        });
        jPanel1.add(jbtnCancel);
        jbtnCancel.setBounds(340, 350, 110, 60);

        jpPanel.add(jPanel1);
        jPanel1.setBounds(20, 20, 560, 410);

        background.setIcon(new javax.swing.ImageIcon(getClass().getResource("/cat20032010/shipbackground(600X450).jpg"))); // NOI18N
        jpPanel.add(background);
        background.setBounds(0, 0, 600, 450);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jpPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 600, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jpPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 450, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jbtnDoneActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnDoneActionPerformed
        try {
            InsertOrder();
            JOptionPane.showMessageDialog(null, "Data Insert Success", "Success!", JOptionPane.INFORMATION_MESSAGE);
            this.setVisible(false);
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, "Data Insert Fail", "Fail!", JOptionPane.ERROR_MESSAGE);
            Logger.getLogger(AddOrder.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jbtnDoneActionPerformed

    private void jcbMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jcbMenuActionPerformed
        try {
            ResultSet rs;
            Main.getConnection();
            Statement stmt = Main.con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
            String MenuCode = (String) jcbMenu.getSelectedItem();
            String SQL = "select menu_name from menu where menu_code = '" + MenuCode + "'";
            rs = stmt.executeQuery(SQL);
            rs.first();
            jtfMName.setText(rs.getString("menu_name"));
        } catch (SQLException ex) {
            Logger.getLogger(AddOrder.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jcbMenuActionPerformed

    private void jbtnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnCancelActionPerformed
        setVisible(false);
    }//GEN-LAST:event_jbtnCancelActionPerformed

    private void jtfQtyInputMethodTextChanged(java.awt.event.InputMethodEvent evt) {//GEN-FIRST:event_jtfQtyInputMethodTextChanged
        
    }//GEN-LAST:event_jtfQtyInputMethodTextChanged

    private void jtfQtyKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jtfQtyKeyPressed
       

    }//GEN-LAST:event_jtfQtyKeyPressed

    private void jtfQtyKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jtfQtyKeyReleased

        double quantity = (double)Double.parseDouble(jtfQty.getText());
        double oriprice = 0;
        try {
            ResultSet rs;
            Main.getConnection();
            Statement stmt = Main.con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
            String MenuCode = (String) jcbMenu.getSelectedItem();
            String SQL = "select menu_price from menu where menu_code = '" + MenuCode + "'";
            rs = stmt.executeQuery(SQL);
            rs.first();
            oriprice = (double) Double.parseDouble(rs.getString("menu_price"));
        } catch (SQLException ex) {
            Logger.getLogger(AddOrder.class.getName()).log(Level.SEVERE, null, ex);
        }
        ttlprice = quantity * oriprice;
        jtfTotalPrice.setText(Double.toString(ttlprice));
    }//GEN-LAST:event_jtfQtyKeyReleased

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    new AddOrder().setVisible(true);
                } catch (SQLException ex) {
                    Logger.getLogger(AddOrder.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }

    public void update() {
        Date currentDate = new Date();
        Date currentTime = new Date();

        time++;
        SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
        SimpleDateFormat formattertime = new SimpleDateFormat("hh:mm:ss");
        String dateString = formatter.format(currentTime);
        String timeString = formattertime.format(currentDate);
        jtfDate.setText(dateString);
        jtfTime.setText(timeString);
        //JOptionPane.showMessageDialog(null, "test3");
    }

    public int time = 0;
    public Timer timer1;

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel background;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JButton jbtnCancel;
    private javax.swing.JButton jbtnDone;
    private javax.swing.JComboBox jcbMenu;
    private javax.swing.JLabel jlTable;
    private javax.swing.JPanel jpPanel;
    private javax.swing.JTextField jtfDate;
    private javax.swing.JTextField jtfMName;
    private javax.swing.JTextField jtfQty;
    private javax.swing.JTextField jtfTable;
    private javax.swing.JTextField jtfTime;
    private javax.swing.JTextField jtfTotalPrice;
    // End of variables declaration//GEN-END:variables

   private class TimerListener implements ActionListener{
       public void actionPerformed(ActionEvent e){
           update();
	   }
   }

    public void insertTableNum(String str) {
    jtfTable.setText(str);
    }

    public void InsertOrder() throws SQLException{
    ResultSet rs;
    Main.getConnection();
    Statement stmt = Main.con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
    String SQL = "insert into ORDER_LIST values ('"+jtfTable.getText()+"','"
            +jcbMenu.getSelectedItem()+"','"+jtfMName.getText().toUpperCase()+"','"
            +jtfQty.getText()+"','PENDING','U','"
            +jtfTime.getText()+"','','"+jtfDate.getText()+"','"
            +jtfTotalPrice.getText()+"')";
    rs = stmt.executeQuery(SQL);
    }

    public void getMenuCode() throws SQLException{
        ResultSet rs;
        Main.getConnection();
        Statement stmt = Main.con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
        String SQL = "select MENU_CODE from MENU";
         rs = stmt.executeQuery(SQL);
                rs.first();
        jcbMenu.removeAll();
         while(!rs.isLast())
        {
            jcbMenu.addItem(rs.getString(1));
        rs.next();
        }
    }

    public void CalculatePrice() throws SQLException{
        ResultSet rs;
        Main.getConnection();
        Statement stmt = Main.con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
        String SQL = "SELECT MENU_PRICE FROM MENU WHERE MENU_CODE = '" +jcbMenu.getSelectedItem()+"'";
    }

}
